
export function deepClone (source) {
  const targetObj = source.constructor === Array ? [] : {}
  for (let keys in source) {
    if (Object.prototype.hasOwnProperty.call(source, keys)) {
      if (source[keys] && typeof source[keys] === 'object') {
        targetObj[keys] = source[keys].constructor === Array ? [] : {}
        targetObj[keys] = deepClone(source[keys])
      } else {
        targetObj[keys] = source[keys]
      }
    }
  }
  return targetObj
}
/* 节流 */
export function throttle(fn, space) {
  console.log(fn, 'fn')
  let timer = null
  return function () {
    if (!timer) {
      timer = setTimeout(function () {
        timer = null
        fn.apply(this, arguments)
      }, space)
    }
  }
}

// let throttleShowLog = throttle(showLog, 3000);

/* 树形菜单获得父级 */
export function getParents(data, menuCode) {
  for (const i in data) {
    if (data[i].menuCode === menuCode) {
      return [data[i]]
    }
    if (data[i].subMenuList) {
      const result = getParents(data[i].subMenuList, menuCode)
      if (result !== undefined) {
        return result.concat(data[i])
      }
    }
  }
}

/* 树形菜单清空选择 */
export function clearSelect (arry) {
  for (let i = 0; i < arry.length; i++) {
    arry[i].selected = false
    if (arry[i].subMenuList && arry[i].subMenuList.length > 0) {
      clearSelect(arry[i].subMenuList)
    }
  }
}
/* 树形菜单清空选择 */
export function clearExpand (arry) {
  for (let i = 0; i < arry.length; i++) {
    arry[i].expanded = false
    if (arry[i].subMenuList && arry[i].subMenuList.length > 0) {
      clearExpand(arry[i].subMenuList)
    }
  }
}
/* 树形菜单展开，选择 */
export function setExpand(data, menuCode) {
  for (const i in data) {
    if (data[i].menuCode === menuCode) {
      data[i].expanded = true
      data[i].selected = true
      return [data[i]]
    }
    if (data[i].subMenuList) {
      const result = setExpand(data[i].subMenuList, menuCode)
      if (result !== undefined) {
        data[i].expanded = true
        data[i].selected = true
        return result.concat(data[i])
      }
    }
  }
}
/* 树形菜单搜索 */
export function filterMenu (value, arr) {
  let newArr = []
  arr.forEach(item => {
    if (item.menuName.indexOf(value) > -1) {
      // item.expanded = true
      newArr.push(item)
    } else {
      if (item.subMenuList && item.subMenuList.length > 0) {
        let redata = filterMenu(value, item.subMenuList)
        if (redata && redata.length > 0) {
          // item.expanded = true
          let obj = {
            ...item,
            subMenuList: redata
          }
          newArr.push(obj)
        }
      }
    }
  })
  return newArr
}